package com.taobao.ic.ticket.test;

import java.util.Date;

import junit.framework.Assert;

import org.junit.Test;

import com.taobao.ic.ticket.constants.TicketConstants;
import com.taobao.ic.ticket.domain.OrderDO;
import com.taobao.ic.ticket.domain.TicketDO;
import com.taobao.ic.ticket.domain.TicketQueryOptionsDO;
import com.taobao.ic.ticket.domain.TicketSkuDO;
import com.taobao.ic.ticket.result.ResultDO;

public class OrderTestCase extends SpringTestCase {

	@Test
	public void testBuyTicket_with_refresh_quantity_to_db() {
		Assert.assertNotNull(orderService);

		TicketDO ticket = new TicketDO();
		ticket.setArriveStationId(1000);
		ticket.setArriveTime(new Date());
		ticket.setBeginStationId(1001);
		ticket.setBeginTime(new Date());
		ticket.setSellerId(1001);
		ticket.setTicketStatus(TicketConstants.TICKET_STATUS_NORMAL);
		ticket.setTrainNum("G28");
		ticket.setTrainType(TicketConstants.TRAIN_TYPE_G);

		TicketSkuDO ticketSkuA = new TicketSkuDO();
		ticketSkuA.setPrice(1000);
		ticketSkuA.setQuantity(7);
		ticketSkuA.setTicketType(TicketConstants.TICKET_TYPE_SOFTBED);
		ticket.getTicketSkuList().add(ticketSkuA);

		TicketSkuDO ticketSkuB = new TicketSkuDO();
		ticketSkuB.setPrice(2000);
		ticketSkuB.setQuantity(7);
		ticketSkuB.setTicketType(TicketConstants.TICKET_TYPE_HARDBED);
		ticket.getTicketSkuList().add(ticketSkuB);

		ticket.setQuantity(14);

		ResultDO<TicketDO> result = ticketService.publishTicket(ticket);
		Assert.assertTrue(result.isSuccess());
		TicketDO ticketDO = result.getModel();

		TicketQueryOptionsDO q = new TicketQueryOptionsDO();
		q.setIncludeSku(true);
		ResultDO<TicketDO> result1 = ticketService.queryTicketById(ticketDO
				.getTicketId(), q);
		Assert.assertTrue(result1.isSuccess());
		
		Assert.assertTrue(ticketDO.getQuantity() == 14);

		

		ResultDO<OrderDO> r = orderService.buyTicket(ticketDO.getTicketId(),
				ticketDO.getTicketSkuList().get(0).getSkuId(), 1000, 3, 100);
		Assert.assertTrue(r.isSuccess());

		TicketQueryOptionsDO q2 = new TicketQueryOptionsDO();
		q.setIncludeSku(true);
		ResultDO<TicketDO> result2 = ticketService.queryTicketById(ticketDO
				.getTicketId(), q2);
		Assert.assertTrue(result2.isSuccess());

		TicketDO ticketDO2 = result2.getModel();
		Assert.assertTrue(ticketDO2.getQuantity() == 11);

	}
	
	@Test
	public void testBuyTicket() {
		Assert.assertNotNull(orderService);

		TicketDO ticket = new TicketDO();
		ticket.setArriveStationId(1000);
		ticket.setArriveTime(new Date());
		ticket.setBeginStationId(1001);
		ticket.setBeginTime(new Date());
		ticket.setSellerId(1001);
		ticket.setTicketStatus(TicketConstants.TICKET_STATUS_NORMAL);
		ticket.setTrainNum("G28");
		ticket.setTrainType(TicketConstants.TRAIN_TYPE_G);

		TicketSkuDO ticketSkuA = new TicketSkuDO();
		ticketSkuA.setPrice(1000);
		ticketSkuA.setQuantity(500);
		ticketSkuA.setTicketType(TicketConstants.TICKET_TYPE_SOFTBED);
		ticket.getTicketSkuList().add(ticketSkuA);

		TicketSkuDO ticketSkuB = new TicketSkuDO();
		ticketSkuB.setPrice(2000);
		ticketSkuB.setQuantity(100);
		ticketSkuB.setTicketType(TicketConstants.TICKET_TYPE_HARDBED);
		ticket.getTicketSkuList().add(ticketSkuB);

		ResultDO<TicketDO> result = ticketService.publishTicket(ticket);
		Assert.assertTrue(result.isSuccess());

		TicketDO ticketDO = result.getModel();

		for (int i = 0; i < 1; i++) {
			ResultDO<OrderDO> r = orderService.buyTicket(
					ticketDO.getTicketId(), ticketDO.getTicketSkuList().get(0)
							.getSkuId(), 1000, 1, 100);
			Assert.assertTrue(r.isSuccess());
		}

	}
	
}
